package ph.gov.marikina.mcsmtms.rest.webservices;

import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import ph.gov.marikina.mcsmtms.domain.MobileUserReport;
import ph.gov.marikina.mcsmtms.domain.SituationReport;
import ph.gov.marikina.mcsmtms.domain.User;
import ph.gov.marikina.mcsmtms.services.SituationReportService;

@Controller
public class SituationReportController {

	@Autowired
	private View jsonView_i;

	@Autowired
	private SituationReportService situationReportService;

	@RequestMapping(value = "/getLoggedUserName/", method = RequestMethod.GET)
	public ModelAndView getLoggedUserName() {

		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		String name = auth.getName(); // get logged in username

		// model.addAttribute("username", name);
		ModelAndView mav = new ModelAndView(jsonView_i,
				BindingResult.MODEL_KEY_PREFIX + "getLoggedUserName",
				name);
		return mav;
	}

	@RequestMapping(value = "/situationreports/", method = RequestMethod.GET)
	public ModelAndView getAllSituationReports() {

		List<SituationReport> situationReportList = situationReportService
				.getAllSituationReports();

		ModelAndView mav = new ModelAndView(jsonView_i,
				"situationReportList",
				situationReportList);
		return mav;
	}
	
	@RequestMapping(value = "/hazardmap/", method = RequestMethod.GET)
	public ModelAndView getAllHazardMapReports() {

		List<SituationReport> hazardMapList = situationReportService.getHazardMapSituationReports();

		ModelAndView mav = new ModelAndView(jsonView_i,
				"hazardMapList",
				hazardMapList);
		return mav;
	}
	
	@RequestMapping(value = "/hazardmap/{direction}", method = RequestMethod.GET)
	public ModelAndView getAllHazardMapReportsForDirection( @PathVariable( "direction" ) String direction) {
		List<SituationReport> hazardMapList = situationReportService.getHazardMapSituationReports(direction);

		ModelAndView mav = new ModelAndView(jsonView_i,
				"hazardMapList",
				hazardMapList);
		return mav;
	}
	
	@RequestMapping(value = "/notifications/", method = RequestMethod.GET)
	public ModelAndView getAllForValidationNotifications() {
		List<SituationReport> notificationList = situationReportService.getForValidationNotifications();

		ModelAndView mav = new ModelAndView(jsonView_i,
				"notificationList",
				notificationList);
		return mav;
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/notifications/road/")
	@ResponseStatus( HttpStatus.CREATED  )
	public ModelAndView getNotificationsForRoad(@RequestBody String requestBody) {
		//Gson gson = new Gson();
        //JsonElement jElement = new JsonParser().parse(receivedStr);
        //MobileUserReport mobileUserReport = gson.fromJson(jElement, new TypeToken<MobileUserReport>(){}.getType());
        
        // create mobileUserReport
        SituationReport approvedSituationReport = null;
        String situationId = "";
        try {
        	//byte[] decoded = Base64.decodeBase64(requestBody.split("=")[1]);
    		situationId = requestBody.split("=")[1];//new String(decoded);
    		
        	approvedSituationReport = situationReportService.approveSituation(situationId);
        } catch(Exception ex) {
        	ex.printStackTrace();

    		ModelAndView mav = new ModelAndView(jsonView_i,
    												"approvedSituationReport",
    												situationId);
    		return mav;
        }

		ModelAndView mav = new ModelAndView(jsonView_i,
												"approvedSituationReport",
												approvedSituationReport);
		return mav;
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/notifications/approve/")
	@ResponseStatus( HttpStatus.CREATED  )
	public ModelAndView approveSituationReport(@RequestBody String requestBody) {
		//Gson gson = new Gson();
        //JsonElement jElement = new JsonParser().parse(receivedStr);
        //MobileUserReport mobileUserReport = gson.fromJson(jElement, new TypeToken<MobileUserReport>(){}.getType());
        
        // create mobileUserReport
        SituationReport approvedSituationReport = null;
        String situationId = "";
        try {
        	//byte[] decoded = Base64.decodeBase64(requestBody.split("=")[1]);
    		situationId = requestBody.split("=")[1];//new String(decoded);
    		
        	approvedSituationReport = situationReportService.approveSituation(situationId);
        } catch(Exception ex) {
        	ex.printStackTrace();

    		ModelAndView mav = new ModelAndView(jsonView_i,
    												"approvedSituationReport",
    												situationId);
    		return mav;
        }

		ModelAndView mav = new ModelAndView(jsonView_i,
												"approvedSituationReport",
												approvedSituationReport);
		return mav;
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/notifications/reject/")
	@ResponseStatus( HttpStatus.CREATED  )
	public ModelAndView rejectSituationReport(@RequestBody String requestBody) {
		//Gson gson = new Gson();
		String situationId = "";

		SituationReport rejectedSituationReport = null;
		
        //JsonElement jElement = new JsonParser().parse(receivedStr);
        //MobileUserReport mobileUserReport = gson.fromJson(jElement, new TypeToken<MobileUserReport>(){}.getType());
        
        // create mobileUserReport
		
		try {
			//byte[] decoded = Base64.decodeBase64(requestBody.split("=")[1]);
			situationId = requestBody.split("=")[1];//new String(decoded);
			
			rejectedSituationReport = situationReportService.rejectSituation(situationId);
		} catch(Exception ex) {
			ex.printStackTrace();
			ModelAndView mav = new ModelAndView(jsonView_i,
					"rejectedSituationReport",
					situationId);
			return mav;
		}
        
		ModelAndView mav = new ModelAndView(jsonView_i,
												"rejectedSituationReport",
												rejectedSituationReport);
		return mav;
	}
	
	@RequestMapping(value = "/notifications/{road}", method = RequestMethod.GET)
	public ModelAndView getUser( @PathVariable( "road" ) String road) {
		List<SituationReport> notificationList = situationReportService.getForValidationNotifications(road);

		ModelAndView mav = new ModelAndView(jsonView_i,
				"notificationList",
				notificationList);
		return mav;
	}

}
